/*
 * @Description: ------------ fileDescription -----------
 * @Author: snows_l snows_l@163.com
 * @Date: 2024-04-25 14:34:48
 * @LastEditors: snows_l snows_l@163.com
 * @LastEditTime: 2024-12-18 14:27:53
 * @FilePath: \Backstage\Server\src\router\menu.js
 */
const express = require('express');
const db = require('../utils/connDB');
const { handleError } = require('../utils/common.js');
const { verifyToken } = require('../utils/handleToken');

const router = express.Router();

// 菜单查询
router.get('/list', async (req, res) => {
  let token = req.cookies['bs-cookie'];
  try {
    const user = verifyToken(token);
    if (!user) throw new Error('JsonWebTokenError:token验证失败');
    const { menu_name, status } = req.query;
    let sql = `SELECT * FROM sys_menu`;
    if (menu_name) {
      sql += ` WHERE menu_name LIKE '%${menu_name}%'`;
    }
    if (status) {
      sql += menu_name ? ` AND status = ${status}` : ` WHERE status = ${status}`;
    }
    const ressltSql = sql + (!menu_name && !status ? ' WHERE status = 1 ORDER BY menu_sort ASC' : ` ORDER BY menu_sort ASC`);
    db.queryAsync(ressltSql)
      .then(result => {
        res.send({
          code: 200,
          data: result.results,
          msg: '查询成功'
        });
      })
      .catch(err => {
        handleError(res, err);
      });
  } catch (error) {
    handleError(res, error);
  }
});

// 菜单新增
router.post('/add', async (req, res) => {
  let token = req.cookies['bs-cookie'];
  try {
    const user = verifyToken(token);
    if (!user) throw new Error('JsonWebTokenError:token验证失败');
    if (user.role_key !== 'admin') throw new Error('ROLE_ERROR:权限不足');
    const { menu_name, parent_id, menu_type, icon, status, menu_sort, path, component, query, visible } = req.body;

    const sql = `INSERT INTO sys_menu (menu_name, parent_id, menu_type, icon, status, menu_sort, path,component, query, visible, create_time) VALUES ('${menu_name}', ${parent_id}, '${menu_type}', '${icon}', ${status}, ${menu_sort}, '${path}', '${component}', '${query}', '${visible}', ?)`;
    db.queryAsync(sql, [new Date()])
      .then(result => {
        res.send({
          code: 200,
          data: result.insertId,
          msg: '新增成功'
        });
      })
      .catch(err => {
        handleError(res, err);
      });
  } catch (error) {
    handleError(res, error);
  }
});

// 菜单修改
router.put('/edit', async (req, res) => {
  let token = req.cookies['bs-cookie'];
  try {
    const user = verifyToken(token);
    if (!user) throw new Error('JsonWebTokenError:token验证失败');
    if (user.role_key !== 'admin') throw new Error('ROLE_ERROR:权限不足');
    const { menu_id, menu_name, parent_id, menu_type, icon, status, menu_sort, path, component, query, visible } = req.body;

    const sql = `UPDATE sys_menu SET menu_name = '${menu_name}', parent_id = ${parent_id}, menu_type = '${menu_type}', icon = '${icon}', status = ${status},menu_sort = ${menu_sort}, path = '${path}', component = '${component}', query = '${query}', visible = ${visible},create_time = ? WHERE menu_id = ${menu_id}`;
    db.queryAsync(sql, [new Date()])
      .then(result => {
        res.send({
          code: 200,
          data: result.affectedRows,
          msg: '修改成功'
        });
      })
      .catch(err => {
        handleError(res, err);
      });
  } catch (error) {
    handleError(res, error);
  }
});

// 菜单删除
router.delete('/delete/:menu_id', async (req, res) => {
  let token = req.cookies['bs-cookie'];
  try {
    const user = verifyToken(token);
    if (!user) throw new Error('JsonWebTokenError:token验证失败');
    if (user.role_key !== 'admin') throw new Error('ROLE_ERROR:权限不足');
    const { menu_id } = req.params;

    const sql = `DELETE FROM sys_menu WHERE menu_id = ${menu_id}`;
    db.queryAsync(sql)
      .then(result => {
        res.send({
          code: 200,
          data: result.affectedRows,
          msg: '删除成功'
        });
      })
      .catch(err => {
        handleError(res, err);
      });
  } catch (error) {
    handleError(res, error);
  }
});

module.exports = router;
